﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace LeadChina.AssemblyLine.Common.UDP
{
    public class InjectionSingleUDP
    {
        //注液泵地址
        private static List<IPEndPoint> targetIPEs = new List<IPEndPoint>
        {
            new IPEndPoint(IPAddress.Parse("192.168.250.201"), 9600),
            new IPEndPoint(IPAddress.Parse("192.168.250.202"), 9600),
            new IPEndPoint(IPAddress.Parse("192.168.250.203"), 9600),
            new IPEndPoint(IPAddress.Parse("192.168.250.204"), 9600),
            new IPEndPoint(IPAddress.Parse("192.168.250.205"), 9600),
            new IPEndPoint(IPAddress.Parse("192.168.250.206"), 9600),
            new IPEndPoint(IPAddress.Parse("192.168.250.207"), 9600),
            new IPEndPoint(IPAddress.Parse("192.168.250.208"), 9600),
            new IPEndPoint(IPAddress.Parse("192.168.250.209"), 9600),
            new IPEndPoint(IPAddress.Parse("192.168.250.210"), 9600),
            new IPEndPoint(IPAddress.Parse("192.168.250.211"), 9600),
            new IPEndPoint(IPAddress.Parse("192.168.250.212"), 9600),
            new IPEndPoint(IPAddress.Parse("192.168.250.213"), 9600),
            new IPEndPoint(IPAddress.Parse("192.168.250.214"), 9600),
            new IPEndPoint(IPAddress.Parse("192.168.250.215"), 9600),
            new IPEndPoint(IPAddress.Parse("192.168.250.216"), 9600),
            new IPEndPoint(IPAddress.Parse("192.168.250.217"), 9600),
            new IPEndPoint(IPAddress.Parse("192.168.250.218"), 9600)
        };


        private static List<byte[]> frameHeaders = new List<byte[]>
        {
            new byte[]{0x80/*ICF*/, 0x00/*RSV固定00*/,0x02/*GCT固定02*/,0x00/*DNA目标网络号*/,0xC9/*DA1目标节点号*/, 0x00/*DA2目标单元号*/, 0x00/*SNA源网络号*/, 0xC7/*SA1源节点号*/,0x00/*SA2源单元号*/,0x00/*SID服务号*/,0x01,0x02/*0101表示读取，0102表示写入*/,},
            new byte[]{0x80/*ICF*/, 0x00/*RSV固定00*/,0x02/*GCT固定02*/,0x00/*DNA目标网络号*/,0xCA/*DA1目标节点号*/, 0x00/*DA2目标单元号*/, 0x00/*SNA源网络号*/, 0xC7/*SA1源节点号*/,0x00/*SA2源单元号*/,0x00/*SID服务号*/,0x01,0x02/*0101表示读取，0102表示写入*/,},
            new byte[]{0x80/*ICF*/, 0x00/*RSV固定00*/,0x02/*GCT固定02*/,0x00/*DNA目标网络号*/,0xCB/*DA1目标节点号*/, 0x00/*DA2目标单元号*/, 0x00/*SNA源网络号*/, 0xC7/*SA1源节点号*/,0x00/*SA2源单元号*/,0x00/*SID服务号*/,0x01,0x02/*0101表示读取，0102表示写入*/,},
            new byte[]{0x80/*ICF*/, 0x00/*RSV固定00*/,0x02/*GCT固定02*/,0x00/*DNA目标网络号*/,0xCC/*DA1目标节点号*/, 0x00/*DA2目标单元号*/, 0x00/*SNA源网络号*/, 0xC7/*SA1源节点号*/,0x00/*SA2源单元号*/,0x00/*SID服务号*/,0x01,0x02/*0101表示读取，0102表示写入*/,},
            new byte[]{0x80/*ICF*/, 0x00/*RSV固定00*/,0x02/*GCT固定02*/,0x00/*DNA目标网络号*/,0xCD/*DA1目标节点号*/, 0x00/*DA2目标单元号*/, 0x00/*SNA源网络号*/, 0xC7/*SA1源节点号*/,0x00/*SA2源单元号*/,0x00/*SID服务号*/,0x01,0x02/*0101表示读取，0102表示写入*/,},
            new byte[]{0x80/*ICF*/, 0x00/*RSV固定00*/,0x02/*GCT固定02*/,0x00/*DNA目标网络号*/,0xCE/*DA1目标节点号*/, 0x00/*DA2目标单元号*/, 0x00/*SNA源网络号*/, 0xC7/*SA1源节点号*/,0x00/*SA2源单元号*/,0x00/*SID服务号*/,0x01,0x02/*0101表示读取，0102表示写入*/,},
            new byte[]{0x80/*ICF*/, 0x00/*RSV固定00*/,0x02/*GCT固定02*/,0x00/*DNA目标网络号*/,0xCF/*DA1目标节点号*/, 0x00/*DA2目标单元号*/, 0x00/*SNA源网络号*/, 0xC7/*SA1源节点号*/,0x00/*SA2源单元号*/,0x00/*SID服务号*/,0x01,0x02/*0101表示读取，0102表示写入*/,},
            new byte[]{0x80/*ICF*/, 0x00/*RSV固定00*/,0x02/*GCT固定02*/,0x00/*DNA目标网络号*/,0xD0/*DA1目标节点号*/, 0x00/*DA2目标单元号*/, 0x00/*SNA源网络号*/, 0xC7/*SA1源节点号*/,0x00/*SA2源单元号*/,0x00/*SID服务号*/,0x01,0x02/*0101表示读取，0102表示写入*/,},
            new byte[]{0x80/*ICF*/, 0x00/*RSV固定00*/,0x02/*GCT固定02*/,0x00/*DNA目标网络号*/,0xD1/*DA1目标节点号*/, 0x00/*DA2目标单元号*/, 0x00/*SNA源网络号*/, 0xC7/*SA1源节点号*/,0x00/*SA2源单元号*/,0x00/*SID服务号*/,0x01,0x02/*0101表示读取，0102表示写入*/,},
            new byte[]{0x80/*ICF*/, 0x00/*RSV固定00*/,0x02/*GCT固定02*/,0x00/*DNA目标网络号*/,0xD2/*DA1目标节点号*/, 0x00/*DA2目标单元号*/, 0x00/*SNA源网络号*/, 0xC7/*SA1源节点号*/,0x00/*SA2源单元号*/,0x00/*SID服务号*/,0x01,0x02/*0101表示读取，0102表示写入*/,},
            new byte[]{0x80/*ICF*/, 0x00/*RSV固定00*/,0x02/*GCT固定02*/,0x00/*DNA目标网络号*/,0xD3/*DA1目标节点号*/, 0x00/*DA2目标单元号*/, 0x00/*SNA源网络号*/, 0xC7/*SA1源节点号*/,0x00/*SA2源单元号*/,0x00/*SID服务号*/,0x01,0x02/*0101表示读取，0102表示写入*/,},
            new byte[]{0x80/*ICF*/, 0x00/*RSV固定00*/,0x02/*GCT固定02*/,0x00/*DNA目标网络号*/,0xD4/*DA1目标节点号*/, 0x00/*DA2目标单元号*/, 0x00/*SNA源网络号*/, 0xC7/*SA1源节点号*/,0x00/*SA2源单元号*/,0x00/*SID服务号*/,0x01,0x02/*0101表示读取，0102表示写入*/,},
            new byte[]{0x80/*ICF*/, 0x00/*RSV固定00*/,0x02/*GCT固定02*/,0x00/*DNA目标网络号*/,0xD5/*DA1目标节点号*/, 0x00/*DA2目标单元号*/, 0x00/*SNA源网络号*/, 0xC7/*SA1源节点号*/,0x00/*SA2源单元号*/,0x00/*SID服务号*/,0x01,0x02/*0101表示读取，0102表示写入*/,},
            new byte[]{0x80/*ICF*/, 0x00/*RSV固定00*/,0x02/*GCT固定02*/,0x00/*DNA目标网络号*/,0xD6/*DA1目标节点号*/, 0x00/*DA2目标单元号*/, 0x00/*SNA源网络号*/, 0xC7/*SA1源节点号*/,0x00/*SA2源单元号*/,0x00/*SID服务号*/,0x01,0x02/*0101表示读取，0102表示写入*/,},
            new byte[]{0x80/*ICF*/, 0x00/*RSV固定00*/,0x02/*GCT固定02*/,0x00/*DNA目标网络号*/,0xD7/*DA1目标节点号*/, 0x00/*DA2目标单元号*/, 0x00/*SNA源网络号*/, 0xC7/*SA1源节点号*/,0x00/*SA2源单元号*/,0x00/*SID服务号*/,0x01,0x02/*0101表示读取，0102表示写入*/,},
            new byte[]{0x80/*ICF*/, 0x00/*RSV固定00*/,0x02/*GCT固定02*/,0x00/*DNA目标网络号*/,0xD8/*DA1目标节点号*/, 0x00/*DA2目标单元号*/, 0x00/*SNA源网络号*/, 0xC7/*SA1源节点号*/,0x00/*SA2源单元号*/,0x00/*SID服务号*/,0x01,0x02/*0101表示读取，0102表示写入*/,},
            new byte[]{0x80/*ICF*/, 0x00/*RSV固定00*/,0x02/*GCT固定02*/,0x00/*DNA目标网络号*/,0xD9/*DA1目标节点号*/, 0x00/*DA2目标单元号*/, 0x00/*SNA源网络号*/, 0xC7/*SA1源节点号*/,0x00/*SA2源单元号*/,0x00/*SID服务号*/,0x01,0x02/*0101表示读取，0102表示写入*/,},
            new byte[]{0x80/*ICF*/, 0x00/*RSV固定00*/,0x02/*GCT固定02*/,0x00/*DNA目标网络号*/,0xDA/*DA1目标节点号*/, 0x00/*DA2目标单元号*/, 0x00/*SNA源网络号*/, 0xC7/*SA1源节点号*/,0x00/*SA2源单元号*/,0x00/*SID服务号*/,0x01,0x02/*0101表示读取，0102表示写入*/,}
        };


        private UDPTool udpTool = new UDPTool(new IPEndPoint(IPAddress.Parse("192.168.250.100"), 7890));


        /// <summary>
        /// 注液量设定
        /// </summary>
        /// <param name="injectionNo">主页站站号</param>
        /// <param name="value">注液量</param>
        /// <returns></returns>
        public bool SendInjectionVolumn(int injectionNo, float value)
        {
            string sendValue = (value * 100).ToString();
            int valueLength = sendValue.Length;
            for (int i = 0; i < 8-valueLength; i++)
            {
                sendValue = '0' + sendValue;
            }

            byte[] textBytes = { 0x82, 0x0D, 0xAC, 0x00, 0x00, 0x02, Convert.ToByte(sendValue.Substring(4, 2), 16), Convert.ToByte(sendValue.Substring(6, 2), 16), Convert.ToByte(sendValue.Substring(0, 2), 16), Convert.ToByte(sendValue.Substring(2, 2), 16) };

            try
            {
                //发送的字节
                byte[] sendBytes = new byte[frameHeaders[injectionNo].Length + textBytes.Length];

                frameHeaders[injectionNo].CopyTo(sendBytes, 0);
                textBytes.CopyTo(sendBytes, frameHeaders[injectionNo].Length);

                udpTool.Send(sendBytes, targetIPEs[injectionNo]);

                byte[] recvBytes = udpTool.Receive();

                if (recvBytes[12] == 0x00 && recvBytes[13] == 0x00)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        /// <summary>
        /// 补偿量设定
        /// </summary>
        /// <param name="injectionNo">主页站站号</param>
        /// <param name="value">补偿量</param>
        /// <returns></returns>
        public bool SendCompensationAmount(int injectionNo, float value)
        {
            string sendValue = (value * 100 + 5000).ToString();
            int valueLength = sendValue.Length;
            for (int i = 0; i < 4 - valueLength; i++)
            {
                sendValue = '0' + sendValue;
            }

            byte[] textBytes = { 0x82, 0x10, 0x04, 0x00, 0x00, 0x01, Convert.ToByte(sendValue.Substring(0, 2), 16), Convert.ToByte(sendValue.Substring(2, 2), 16)};

            try
            {
                //发送的字节
                byte[] sendBytes = new byte[frameHeaders[injectionNo].Length + textBytes.Length];

                frameHeaders[injectionNo].CopyTo(sendBytes, 0);
                textBytes.CopyTo(sendBytes, frameHeaders[injectionNo].Length);

                udpTool.Send(sendBytes, targetIPEs[injectionNo]);

                byte[] recvBytes = udpTool.Receive();

                if (recvBytes[12] == 0x00 && recvBytes[13] == 0x00)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

    }
}
